package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.lanou3g.ptp.api.service.ApiStudentService;
import com.lanou3g.ptp.entity.Student;
import com.lanou3g.ptp.student.service.StudentService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Api(description = "学生接口")
@RestController
public class StudentController {

    @Autowired
    private ApiStudentService apiApiStudentService;

    @Reference
    private StudentService studentService;

    @ApiOperation("查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "显示条数"),
            @ApiImplicitParam(name = "keyword", value = "查询条件")
    })
    @GetMapping("/student")
    private PtpResult wordFindAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "size", defaultValue = "5") Integer size,
                                  String keyword) {
        PageInfo<Student> all = apiApiStudentService.findByKeyword(page, size, keyword);

        return PtpResultUtils.ok(all);
    }


    @ApiOperation("通过id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id")
    })
    @DeleteMapping("/student/{id}")
    private PtpResult delete(@PathVariable Long id) {
        int i = studentService.deleteById(id);
        if (i == 0) {
            return PtpResultUtils.ok(1400);
        } else {
            return PtpResultUtils.ok(200);
        }
    }

    @ApiOperation("通过id 多个删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个学生id")
    })
    @DeleteMapping("/students/{ids}")
    private PtpResult deletes(@PathVariable List<Long> ids) {
        int i = studentService.deleteByIds(ids);
        if (i == 0) {
            return PtpResultUtils.ok(1400);
        } else {
            return PtpResultUtils.ok(200);
        }
    }


    @ApiOperation("修改学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id"),
            @ApiImplicitParam(name = "head", value = "头像"),
            @ApiImplicitParam(name = "schoolId", value = "学校id"),
            @ApiImplicitParam(name = "collegeId", value = "学院id"),
            @ApiImplicitParam(name = "majorId", value = "专业id"),
            @ApiImplicitParam(name = "clazzId", value = "班级id"),
            @ApiImplicitParam(name = "createTime", value = "创建时间"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "user.createTime", value = "用户表创建时间"),
            @ApiImplicitParam(name = "user.id", value = "用户id"),
            @ApiImplicitParam(name = "user.name", value = "用户名字"),
            @ApiImplicitParam(name = "user.no", value = "用户学工号"),
            @ApiImplicitParam(name = "user.password", value = "用户密码"),
            @ApiImplicitParam(name = "user.status", value = "用户状态"),
            @ApiImplicitParam(name = "user.tel", value = "用户手机号"),
            @ApiImplicitParam(name = "user.updateTime", value = "用户表更新时间")
    })
    @PutMapping("/student/{id}")
    private PtpResult upStudent(@PathVariable Long id, Student student) {
        int i = 0;
        try {
            i = studentService.upStuById(id, student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i == 0) {
            return PtpResultUtils.ok(1400);
        } else {
            return PtpResultUtils.ok(200);
        }
    }

    @ApiOperation("添加信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生id"),
            @ApiImplicitParam(name = "head", value = "头像"),
            @ApiImplicitParam(name = "schoolId", value = "学校id"),
            @ApiImplicitParam(name = "collegeId", value = "学院id"),
            @ApiImplicitParam(name = "majorId", value = "专业id"),
            @ApiImplicitParam(name = "clazzId", value = "班级id"),
            @ApiImplicitParam(name = "createTime", value = "创建时间"),
            @ApiImplicitParam(name = "updateTime", value = "修改时间"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "user.createTime", value = "用户表创建时间"),
            @ApiImplicitParam(name = "user.id", value = "用户id"),
            @ApiImplicitParam(name = "user.name", value = "用户名字"),
            @ApiImplicitParam(name = "user.no", value = "用户学工号"),
            @ApiImplicitParam(name = "user.password", value = "用户密码"),
            @ApiImplicitParam(name = "user.status", value = "用户状态"),
            @ApiImplicitParam(name = "user.tel", value = "用户手机号"),
            @ApiImplicitParam(name = "user.updateTime", value = "用户表更新时间")
    })
    @PostMapping("/student")
    private PtpResult addStudent(Student student) {
        int i = 0;
        try {
            i = studentService.insetStudent(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i == 0) {
            return PtpResultUtils.ok(1400);
        } else {
            return PtpResultUtils.ok(200);
        }
    }

    @ApiOperation("导入学生Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentUploadFile" ,value = "学生信息Excel表.xlsx")
    })
    @PostMapping("/student/upload")
    public PtpResult inputStudent(@RequestParam("file") MultipartFile studentUploadFile) throws IOException {
        // 获取文件名
        String name = studentUploadFile.getOriginalFilename();
        //获取文件名字
        //判断文件符不符合要求
        if (name.endsWith("xlsx")) {
            //转成字节数组
            byte[] bytes = studentUploadFile.getBytes();
            //解析数据 放入数据库
            boolean b = studentService.excelToStudentInfo(bytes);
            //返回布尔类型
            return PtpResultUtils.ok(b);
        } else {
            return PtpResultUtils.ok("上传文件格式不正确");
        }
    }

    @ApiOperation("导出学生Excel模板")
    @GetMapping("/student/model")
    public void modelStudent(HttpServletResponse response) {
        response.reset();
        response.setContentType("application/msexcel;charset=utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("学生信息样表.xlsx", "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] bytes = studentService.excelStudentModel();
        try {
            IOUtils.write(bytes,response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
